<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spf" uri="http://www.springframework.org/tags/form"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>凭证数据导入</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
<meta http-equiv="X-UA-Compatible" content="IE=11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width" />

<style type="text/css">
body {font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;margin: 0;padding: 0;}
.sidebar1 {float: left;width: 10%;padding-bottom: 10px;}
.content {padding: 10px 0;width: 90%;float: left;height: 800px;}
a img {border: none;}
a:link {text-decoration: none;}
a:visited {	color: #6E6C64;text-decoration: none;}
a:hover,a:active,a:focus {text-decoration: none;color: red;cursor: pointer}
.divcss5 {border: 1px solid #99CCFF}
.td {border: solid #99CCFF;border-width: 0px 1px 1px 0px;padding: 10px 0px;}
.table {border: solid #99CCFF;border-width: 2px 0px 0px 2px;}
.black_overlay {display: none;position: absolute;top: 0%;left: 0%;width: 60%;
height: 40%;background-color: black;z-index: 1001;-moz-opacity: 0.8;opacity: .80;filter: alpha(opacity = 80);}

.white_content {display: none;position: absolute;top: 20%;left: 20%;width: 60%;height: 40%;border: 16px solid lightblue;
	background-color: white;z-index: 1002;overflow: auto;}
</style>

</head>
<body>
<div style="margin-top: 20px; margin-left: 20px;margin-right: 20px;">
<form name="queryForm" id="queryForm" action="" method="post" target="hidden_frame"  onkeydown="if(event.keyCode==13)return false;"/>
	<table width="600"  border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top: 10px;">
		<tr>
			<td width="80">公司代码：</td>
			<td>&nbsp;<input type="text" name="companyId" id="companyId"  onchange="checkCompany()" 
			style="text-transform: uppercase;"/>
			</td>
		</tr>
		<tr>  
		<td align="center">&nbsp;</td>
		  <td  ><div id="compId"></div></td>
		 
		</tr>
		<tr>
			<td width="80">凭证文件：</td>
			<td>&nbsp;<input type="file" name="fileField" id="fileField" onchange="checkFile();" />
			</td>
		</tr>
		<tr>
		 <td align="center">&nbsp;</td>
		  <td><div id="files"></div></td>
		</tr>
		<tr><td align="center">&nbsp;</td>
			<td  valign="middle">&nbsp;
			<button onclick="upload()">导入</button>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<button onclick="checksReset()">取消</button></td>
		</tr>
		<tr>
			<td height="30" colspan="2" align="center"><div id="mess"></div></td>
		</tr>
	</table>
    </form>
   <iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
	<div class="divcss5" style="margin-left: 20px;margin-right: 20px;">
	<!--  <a href="record/getRuns" title="刷新">-->
	<a href = "javascript:void(0);" onclick ="_refresh()" title="刷新">
	  <img src="<%=basePath%>imgs/refresh.gif" width="32" height="31"> </a>
	     <div id="impos">
		     <table width="100%" class="table">
					<tr align="center">
						<td class="td">序列</td>
						<td class="td">公司代码</td>
						<td class="td">文件名称</td>
						<td class="td">操作对象</td>
						<td class="td">运行状态</td>
						<td class="td">运行时间</td>
						<td class="td">结束时间</td>
						<td class="td">读取行/总行数</td>	 
                        <td class="td">操作</td>
					</tr>
					<c:if test="${not empty fenye.dataList}">
						<c:forEach items="${fenye.dataList}" var="run" varStatus="tst">
								<c:if test="${run.lockFlag=='E'}">
									<tr align="center" bgcolor="#99CCFF">
										<td class="td">${run.recordNo}</td>
										<td class="td">${run.companyId}</td>
										<td class="td">${run.fileName}</td>
										<td class="td">${run.description}</td>
										<td class="td">文件已上传</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
										<td class="td">&nbsp;</td>	
									</tr>
								</c:if>
								
								<c:if test="${run.lockFlag=='R'}">
									<tr align="center" bgcolor="#FFCCFF">
										<td class="td">${run.recordNo }</td>
										<td class="td">${run.companyId}</td>
										<td class="td">${run.fileName}</td>
										<td class="td">${run.description}</td>
										<td class="td">正在运行</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
										<td class="td">&nbsp;</td>	
									</tr>
								</c:if>
								 
								<c:if test="${run.lockFlag=='N'}">
									<tr align="center" bgcolor="#CCCCCC">
										<td class="td">${run.recordNo }</td>
										<td class="td">${run.companyId}</td>
										<td class="td">${run.fileName}</td>
										<td class="td">${run.description}</td>
										<td class="td">文件导出为空</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>	
                                        <td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
                                        	
	                                   <td class="td"><a href="javascript:void(0):" onclick="ShowDiv('${run.recordNo }','fade');" >错误消息</a> </td>
	                            
									</tr>
								</c:if>
		
								
								<c:if test="${run.lockFlag=='X'}">
									<tr align="center" bgcolor="#FF3399">
										<td class="td">${run.recordNo}</td>
									    <td class="td">${run.companyId}</td>
										<td class="td">${run.fileName}</td>
										<td class="td">${run.description}</td>
										<td class="td">文件导出内容错误</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>	
                                       	<td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
											
										<td class="td"><a href="javascript:void(0):" onclick="ShowDiv('${run.recordNo }','fade');" >错误消息</a> </td>
										 
									</tr>
								</c:if>
								 
								<c:if test="${run.lockFlag=='S'}">
									<tr align="center">
										<td class="td">${run.recordNo }</td>
										<td class="td">${run.companyId}</td>
										 <td class="td">${run.fileName}</td> 
										<td class="td">${run.description}</td>
										<td class="td">凭证导入成功</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>	
										<td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
										<td class="td">&nbsp;</td>	
									</tr>
								</c:if>
								
									<c:if test="${run.lockFlag=='T'}">
									<tr align="center" bgcolor="yellow">
										<td class="td">${run.recordNo }</td>
										<td class="td">${run.companyId}</td>
										 <td class="td">${run.fileName}</td> 
										<td class="td">${run.description}</td>
										<td class="td">更新失败</td>
										<td class="td"><fmt:formatDate value="${run.runTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										<td class="td"><fmt:formatDate value="${run.endTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>	
                                       	<td class="td"><fmt:formatNumber value="${run.rowLines}" pattern="#" type="number"/> / <fmt:formatNumber value="${run.numLines}" pattern="#" type="number"/></td>
                                        	
										<td class="td"><a href="javascript:if(confirm('确实要重试吗吗?'))location='/HFG/record/${run.infoId}/reset'">重试</a>
										||<a href="javascript:void(0):" onclick="ShowDiv('${run.recordNo }','fade');" >错误消息</a> 
									 </td>
									</tr>
								</c:if>
								<div id="fade" class="black_overlay"> </div>
										<div id="${run.recordNo }" class="white_content">
										    <div style="text-align: right; cursor: default; height: 40px;" id="move">
										         <span style="font-size: 16px;" onclick="CloseDiv('${run.recordNo }','fade');">关闭</span>
										     </div>
										        ${run.message }
										</div>
						</c:forEach>
					</c:if>
					<tr>
						<td colspan="6" align="center">${fenye.viewButton}</td>
					</tr>
				</table>
	     </div>
	</div> 
</div>
 

</body>
</html>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.11.3.js"></script>
 

<script type="text/javascript">
	//弹出隐藏层
	function ShowDiv(show_div, bg_div) {
		document.getElementById(show_div).style.display = 'block';
		document.getElementById(bg_div).style.display = 'block';
		var bgdiv = document.getElementById(bg_div);
		bgdiv.style.width = document.body.scrollWidth;
		// bgdiv.style.height = $(document).height();
		$("#" + bg_div).height($(document).height());
	};
	//关闭弹出层
	function CloseDiv(show_div, bg_div) {
		document.getElementById(show_div).style.display = 'none';
		document.getElementById(bg_div).style.display = 'none';
	};
</script>


<script type="text/javascript"  >
function checkCompany(){	
	var labels = document.getElementById("compId");
	var comp = document.getElementById("companyId").value;
 	if (comp.length == 0) {
		document.getElementById("companyId").style.border = "2px solid  red";
		labels.innerHTML = "<font color='red'>公司代码不能为空</font>";
		return false;
	} else {
		$.ajax({
			type : "GET",
			url : "/HFG/comp/check",
			data : {
				companyId : $("#companyId").val()
			},
			dataType : "json",
			success : function(data) {
				if (data.result == "error") {
					document.getElementById("companyId").style.border = "2px solid  red";
					labels.innerHTML = "<font color='red'>"	+ data.message + "</font>";
					return false;
				} else {
					labels.innerHTML = "<font color='green'>"+ data.message + "</font>";
					document.getElementById("companyId").style.border = "2px solid  green";
					return true;
				}
			}
		});
	}
};

function checkFile() {
	var labels = document.getElementById("files");
	var file = document.getElementById("fileField").value;
	var companyId = document.getElementById("companyId").value;
	 if (typeof String.prototype.startsWith != 'function') { 
		 String.prototype.startsWith = function (str){
         return this.slice(0, str.length) == str;
      };
    }
　　　
    // 判断当前字符串是否以str结束
    if (typeof String.prototype.endsWith != 'function') {
      String.prototype.endsWith = function (str){
         return this.slice(-str.length) == str;
      };
    }
    // 测试程序
    var sBeCompareStr = ".txt";// 被比较字符串

	if (file.length == 0) {
		document.getElementById("fileField").style.border = "2px solid red";
		labels.innerHTML = "<font color='red'>凭证文件不能为空</font>";
		return false;
	} else {
		var names = file.substring(0, file.lastIndexOf("."));
		var sunms = names.substring(names.lastIndexOf("_") + 1);
		if(file.indexOf("SSC_" + companyId.toUpperCase() + "_") < 0){
 			document.getElementById("fileField").style.border = "2px solid red";
 			labels.innerHTML = "<font color='red'>凭证文件格式不合法,不是SSC_" + companyId.toUpperCase() + "_开头的文件</font>";
 			return false;
		}  else if( file.endsWith(sBeCompareStr)==false){
 			document.getElementById("fileField").style.border = "2px solid red";
 			labels.innerHTML = "<font color='red'>凭证文件格式不合法,不是.txt文件</font>";
 			return false;
		} else if(isNaN(sunms) == true){
			document.getElementById("fileField").style.border = "2px solid red";
 			labels.innerHTML = "<font color='red'>凭证文件格式不合法,"+sunms+"不是正整数</font>";
 			return false;
		} else if(sunms.length > 14|| sunms.length < 14){
			document.getElementById("fileField").style.border = "2px solid red";
			labels.innerHTML = "<font color='red'>凭证文件格式不合法,"+sunms+"长度不够14位</font>";
			return false;
		}else{
		    var pos=file.lastIndexOf("\\");
			labels.innerHTML = "<font color='green'>"+ file.substring(pos+1) + "</font>";
			document.getElementById("fileField").style.border = "2px solid green";
			return true;
		}
	}
};


function upload(){
	 if(checkCompany()==false||checkFile()==false){
		 return false;
	 }
	var labels = document.getElementById("mess");
	var formData = new FormData($("#queryForm")[0]);
	$.ajax({
		url : '/HFG/record/uploadFile',
		type : 'POST',
		data : formData,
		dataType : "json",
		//async : false,
		contentType : false,
		processData : false,
		success : function(data) {
			if (data.result == "error") {
				_refresh();
				clean_out();
				alert(data.message);
			} else {
				labels.innerHTML = "<font color='green'>"+ data.message + "</font>";
				_refresh();
				clean_out();
			}	
		}
	});
};

function _refresh(){
	 $.post("record/getRunArray",
		 function (msg){
		     $("#impos").html(msg);
		 }
	  );
};


function clean_out(){
 
	document.getElementById("companyId").value="";
	document.getElementById("companyId").style.border = "";
	document.getElementById("compId").innerHTML = "";
	
	document.getElementById("fileField").value="";
	document.getElementById("fileField").style.border = "";
	document.getElementById("files").innerHTML = "";
 
	$("#compId").empty();    
	$("#files").empty();   
    
};

function checksReset(){
	var labels = document.getElementById("compId");
	document.getElementById("companyId").value="";
	document.getElementById("companyId").style.border = "";
	labels.innerHTML = "";
	
	document.getElementById("fileField").value="";

	document.getElementById("fileField").style.border = "";
	$("#compId").empty();    
	$("#files").empty();   
	$("#mess").empty();   
};
</script>
